 document.write("<script language=javascript src='../js/validate.js'></script>");
var app = angular.module('Login', ['ngRoute'])
app.config(['$routeProvider', function($routeProvider) {
	$routeProvider.when('/login', {
		templateUrl: 'login.html',
		controller: 'loginCtrl'
	});
}]);
app.controller("loginCtrl", function($scope, $location, $http, $window) {
	$scope.removeE = function() {
		$(".has-feedback").removeClass("has-error")
	}
	$('#usernameInput').focus()
	//前往角色权限控制登录页
	$scope.jumpToRoleAuthorityControl = function() {
		window.location.href = 'tenantLogin.html';
	}
	$scope.getJson = function(path, data, method, successfn) {
		$http({
			method: method,
			dataType: "json",
			url: path,
			data: data,
			headers: {
				'Content-Type': 'application/json',
//				'token_id': '11'
			}

		}).
		success(function(data) {
			successfn(data);
		}).
		error(function(data, status) {
			//          	               console.log(data.msg);
		});

	}
	$scope.user = {}
	if( localStorage.corporationName == undefined || localStorage.corporationName == ''){
		$scope.corporationName = '请选择企业'
	}else{
		$scope.corporationName = localStorage.corporationName ;
		$scope.corporationId = localStorage.corporationId;
	}
	
	$scope.corporationCode = null
	//根据企业code查询企业
	$scope.searchCorporation = function(){
		if($scope.corporationCode == null || $scope.corporationCode == undefined){
			return
		}
		console.log($scope.corporationCode)

		$scope.getJson(resultPath+'/v1/tenant/1/enterprise/'+$scope.corporationCode,'','get',function(data){
			console.log(data)
			if(data.code==0){
				$scope.corporation=data.data[0]
				$scope.corporationName = $scope.corporation.name
				$scope.corporationId = $scope.corporation.id
				//查询出来的信息存储起来
				localStorage.corporationId = $scope.corporationId ;
				localStorage.corporationName = $scope.corporationName ;
				$('#chooseC').modal('toggle')
			}else if(data.code==-1 && data.msg == '企业不存在'){
				$('#noC').fadeIn('slow')
				setTimeout("$('#noC').fadeOut('slow')",3000);
			}
		})
	}
	$scope.login = function() {
		if($scope.corporationId == null){
			$('#haveToChoose').fadeIn('slow')
			setTimeout("$('#haveToChoose').fadeOut('slow')",3000);
			return
		}
		$scope.user.enterprise_id = $scope.corporationId
		if($scope.user.user_name == undefined || $scope.user.password == undefined) {
			return
		} else {
			console.log($scope.user);
			$scope.getJson(resultPath+'/v1/login', $scope.user, 'post', function(data) {
				console.log(data)
				if(data.code == 0) {
//					var wsCache = new WebStorageCache();
//					// 手工删除所有超时CacheItem,
//					wsCache.deleteAllExpires();
//					//设置超时时间1800s
//					wsCache.set('real_name', data.data.realName, {exp : 1800});
					localStorage.real_name = data.data.realName ;
					//跳转界面
					window.location.href = "main.html";
				} else {
					$('#errorStr').html(data.msg)
					$('#ErrorUser').fadeIn('slow')
					setTimeout("$('.alert').fadeOut('slow')", 3000);
					$(".has-feedback").addClass("has-error")
				}
			})
		}
	}
	//					图片上传
	//          	$scope.test = function(){
	//          		let file = $('#uiui').get(0).files[0];
	//          		var storage=window.sessionStorage;
	//	           		let token_id=storage.token_id;
	//          		console.log(token_id)
	//				        let fd = new FormData();
	//				        fd.append('file', file); 
	//				        $http({
	//				              method:'POST',
	//				              url:"http://192.168.1.125/v1/base_system/file/upload",
	//				              data: fd,
	//				              
	//				              headers: {
	//				              	'token_id':token_id,
	//				              	'Content-Type':undefined
	//				              },
	//				              transformRequest: angular.identity 
	//				        })   
	//				        .success( function ( data ){
	//				            console.log(data)
	//				            console.log(fd)
	//				        }); 
	//				
	//				   
	//          	}
});